﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestClientRenderService.DTDBService;
using IndicoInterface;
using System.IO;
using ClientWorkflowLibrary;
using System.Threading;

namespace TestClientRenderService
{
    class Program
    {
        /// <summary>
        /// See if the client service code properly renders
        /// a conference.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=a042880";

            ///
            /// Copy over an empty DB for testing
            /// 

            FileInfo db = new FileInfo("ConfData_TestClientRender.sdf");
            File.Copy(@"..\..\..\DeepTalkDBService\ConferenceData.sdf", db.FullName, true);
            using (DTDBClient rc = new DTDBClient())
            {
                //rc.SetDBFile(db);
            }

            ///
            /// Insert the URL into the database for work...
            /// 

            AgendaInfo info = new AgendaInfo(url);
            StringWriter sr = new StringWriter();
            info.Seralize(sr);
            string agendaXML = sr.ToString();

            string initialTitle = "Not Rendered Yet";
            using (DTDBClient dcl = new DTDBClient())
            {
                //dcl.SubmitNewTalk(agendaXML, initialTitle, DateTime.Now);
            }

            ///
            /// Great. Now we can actually start the thing up
            /// 

            RenderWFManager mgr = new RenderWFManager();
            mgr.WorkflowFinished += new RenderWFManager.WorkflowFinishedCallback(mgr_WorkflowFinished);
            mgr.Start();
            Console.WriteLine("Started the render manager");

            ///
            /// See if the title gets changed...
            /// 

            Console.WriteLine("Waiting for the title in the db to get changed");
            string title = initialTitle;
            while (title == initialTitle)
            {
                Thread.Sleep(10*1000);
                using (DTDBClient dcl = new DTDBClient())
                {
                    BasicTalkInfo[] talks = dcl.GetUnrenderedTalks(10);
                    if (talks.Length != 1)
                    {
                        Console.WriteLine("ERROR: we see no unrendered talks!");
                        return;
                    }
                    title = talks[0].Title;
                }
            }

            Console.WriteLine("Conference talk title changed to: " + title);

            ///
            /// Now wait for user input to have this "done".
            /// 

            Console.WriteLine("Waiting for the workflow to finish up");
            _done_with_conference.WaitOne();

            ///
            /// Shut down
            /// 

            mgr.Stop();

            ///
            /// Get a list of the rendered talks now - just to show...
            /// 

            using (DTDBClient dcl = new DTDBClient())
            {
                BasicTalkInfo[] talks = dcl.GetRenderedTalks(10);
                foreach (var t in talks)
                {
                    Console.WriteLine("Rendered talk: " + t.Title);
                }
            }
        }

        private static AutoResetEvent _done_with_conference = new AutoResetEvent(false);

        /// <summary>
        /// Workflow managed to finish up - print something out!
        /// </summary>
        /// <param name="e"></param>
        static void mgr_WorkflowFinished(Exception e)
        {
            if (e == null)
            {
                Console.WriteLine("Finished meeting with no errors");
            }
            else
            {
                Console.WriteLine("Finished with an error: " + e.Message);
                Console.WriteLine(e.StackTrace);
            }
            _done_with_conference.Set();
        }
    }
}
